package com.example.administrator.daydayappmarket.software.choice.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.software.choice.activity.SoftwareChoiceSecondActivity;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicApps;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by lenovo on 2016/12/26.
 */

public class SoftwareChoiceListViewAdapter extends BaseAdapter {
    private Context context;
    private List<SoftwareChoiceTopicApps> data;

    public SoftwareChoiceListViewAdapter(Context context, List<SoftwareChoiceTopicApps> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SoftwareChoiceTopicApps getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_software_choice_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final SoftwareChoiceTopicApps softwareChoiceTopicApps = data.get(position);
        viewHolder.itemSoftwareChoiceListViewImageView.setImageResource(R.mipmap.app_default);
        if (softwareChoiceTopicApps.getIcon_url().startsWith("http")) {
            Picasso.with(context).load(softwareChoiceTopicApps.getIcon_url()).into(viewHolder.itemSoftwareChoiceListViewImageView);
        } else {
            Picasso.with(context).load(Contacts.FRONT_IMG + softwareChoiceTopicApps.getIcon_url()).into(viewHolder.itemSoftwareChoiceListViewImageView);
        }
        viewHolder.itemSoftwareChoiceListViewNameText.setText(softwareChoiceTopicApps.getName());

        int personCount = softwareChoiceTopicApps.getDownload_count() / 10000;
        viewHolder.itemSoftwareChoiceListViewPersonText.setText(personCount + "万人下载");

        double size = (double) softwareChoiceTopicApps.getPackage_size() / 1024 / 1024;
        DecimalFormat df = new DecimalFormat("####0.00");
        String format = df.format(size);
        viewHolder.itemSoftwareChoiceListViewSizeText.setText(format + "MB");

        viewHolder.itemSoftwareChoiceListViewReviewText.setText(softwareChoiceTopicApps.getReviews());

        viewHolder.itemSoftwareChoiceListViewRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = softwareChoiceTopicApps.getId();
                Intent intent=new Intent(context,SoftwareChoiceSecondActivity.class);
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });


        return convertView;
    }


    static class ViewHolder {
        @InjectView(R.id.item_software_choice_listView_imageView)
        ImageView itemSoftwareChoiceListViewImageView;
        @InjectView(R.id.item_software_choice_listView_nameText)
        TextView itemSoftwareChoiceListViewNameText;
        @InjectView(R.id.item_software_choice_listView_reviewText)
        TextView itemSoftwareChoiceListViewReviewText;
        @InjectView(R.id.item_software_choice_listView_personText)
        TextView itemSoftwareChoiceListViewPersonText;
        @InjectView(R.id.item_software_choice_listView_sizeText)
        TextView itemSoftwareChoiceListViewSizeText;
        @InjectView(R.id.item_software_choice_listView_button)
        TextView itemSoftwareChoiceListViewButton; //将Button更改为TextView
        @InjectView(R.id.item_software_choice_listView_relative)
        RelativeLayout itemSoftwareChoiceListViewRelative;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }


}
